//
//  ZXTitleBtn.m
//  ZXOCWeiBo
//
//  Created by mac on 15/11/29.
//  Copyright © 2015年 itcast. All rights reserved.
//

#import "ZXTitleBtn.h"

#define kImgWith 10 //图片宽度

@implementation ZXTitleBtn

//初始化控件
-(instancetype)initWithFrame:(CGRect)frame
{
    
    if (self = [super initWithFrame:frame])
    {
        
        [self setBackgroundImage:[UIImage imageNamed:@"tabbar_background"] forState:UIControlStateHighlighted];
        
        //设置按钮的图片
        [self setImage:[UIImage imageNamed:@"new_dot"] forState:UIControlStateNormal];
        [self setImage:[UIImage imageNamed:@"new_dot"] forState:UIControlStateHighlighted];
        
        [self setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        
//        self.backgroundColor = [UIColor purpleColor];
        
        //设置图片不被 拉伸
        self.imageView.contentMode = UIViewContentModeCenter;
        
        //文字居中显示
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        
        //设置字体大小
        self.titleLabel.font = [UIFont systemFontOfSize:14];
        
        
        //如果美工没有给圆角的背景图片，可以自己用代码设置
        self.layer.cornerRadius = 5;
        self.layer.masksToBounds = YES;
    }
    return self;
   
}

//添加子控件，或者设置View的时候会调用这个方法
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    //重新设置按钮w
//    
// CGSize titleSize = [self.titleLabel.text sizeWithAttributes:@{NSFontAttributeName:self.titleLabel.font}];
//    
//    CGFloat selfw = titleSize.width + kImgWith + 5;
//    self.w = selfw;
//    self.h = titleSize.height + 4;
    
    //2.titleView frm
    CGFloat titleX = 0;
    CGFloat titleY = 0;
    CGFloat titleW = self.titleLabel.frame.size.width;
    CGFloat titleH = self.titleLabel.frame.size.height;
    self.titleLabel.frame = CGRectMake(titleX, titleY, titleW, titleH);
    
    //重新布局titleView和imageView
//    1.imageView frm
    CGFloat imgX = titleW;
    CGFloat imgY = self.imageView.frame.origin.y;
    CGFloat imgW = self.imageView.frame.size.width;
    CGFloat imgH = self.imageView.frame.size.height;
    self.imageView.frame = CGRectMake(imgX, imgY, imgW, imgH);
    
    
}
@end
